package cn.ydt.ydt.community.common.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 获得定位的详细位置
 *
 * @author Administrator
 */
public class GetLocationDetailUtils {


    /**
     * 高德地图API接口
     */
    private static final String GD_URL="https://restapi.amap.com/v5/direction/driving";
    /**
     * 高德地图key
     */
    private static final String GD_KEY="f2e26c3438c7f8b6f711deb3d8af1d93";

    /**
     * 百度地图API接口
     */
    private static final String URL = "https://api.map.baidu.com/reverse_geocoding/v3/";
    /**
     * 用户申请应用的key
     */
    private static final String AK = "l8A2ZhQ56NXjr1OuhCDghOaFLhVtBFyL";

    private GetLocationDetailUtils() {
        throw new AssertionError("不能产生实例");
    }


    /**
     * 百度地图通过经纬度来获取地址,传入参数纬度lat、经度lng
     *
     * @param lat
     * @param lng
     * @return
     */
    public static JSONObject getCity(String lat, String lng) {

        Map<String, String> paramsMap = new LinkedHashMap<String, String>();
        paramsMap.put("location", lat + "," + lng);
        paramsMap.put("output", "json");
        paramsMap.put("pois", "1");
        paramsMap.put("ak", AK);
        String params = toQueryString(paramsMap);
        JSONObject res = null;
        JSONObject obj = null;
        //发送请求
        res = HttpUtils.doGet(URL, paramsMap);
        //转换为josn字符串
        obj = JSONObject.parseObject(res.toJSONString());
        return obj;
    }

    /**
     * 百度地图通过地址来获取经纬度，传入参数address
     *
     * @param address
     * @return
     */
    public static JSONObject getLngAndLat(String address) {
        Map<String, String> paramsMap = new LinkedHashMap<String, String>();
        paramsMap.put("address", address);
        paramsMap.put("output", "json");
        paramsMap.put("ak", AK);
        String params = toQueryString(paramsMap);
        JSONObject res = null;
        JSONObject obj = null;
        //发送请求
        res = HttpUtils.doGet(URL, paramsMap);
        //转换为josn字符串
        obj = JSONObject.parseObject(res.toJSONString());

        return obj;
    }


    // 对Map内所有value作utf8编码，拼接返回结果
    private static String toQueryString(Map<?, ?> data) {
        StringBuffer queryString = null;
        try {
            queryString = new StringBuffer();
            for (Map.Entry<?, ?> pair : data.entrySet()) {
                queryString.append(pair.getKey() + "=");
                queryString.append(URLEncoder.encode((String) pair.getValue(), "UTF-8") + "&");
            }
            if (queryString.length() > 0) {
                queryString.deleteCharAt(queryString.length() - 1);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return queryString.toString();
    }

    // 来自stackoverflow的MD5计算方法，调用了MessageDigest库函数，并把byte数组结果转换成16进制
    private static String MD5(String md5) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
                        .substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
